﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

/// <summary>
///WikiSystem 的摘要说明
/// </summary>
public class WikiSystem
{
    public WikiSystem(string apppath)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        this.ApplicationPath = apppath;
        if (wikisystem != null)
            throw new Exception("Wiki System should be only 1 instance!");
        wikisystem = this;
	}

    public static WikiSystem wikisystem;
    public string ApplicationPath;

    public GraphBase.GraphDatabase GraphDatabase;

    public void Initialize()
    {
        string graphfile = null;
        for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
        {
            string config = ConfigurationManager.AppSettings[i];
            if(System.IO.File.Exists(config))
                graphfile=config;
            //string graphfile = ConfigurationManager.AppSettings["gdb"];
        }
        if(graphfile == null)
            graphfile = this.ApplicationPath+@"gdb\NaturalWiki.gdb";
        this.GraphDatabase = new GraphBase.GraphDatabase(graphfile);
        this.GraphDatabase.WebAppDirectory = this.ApplicationPath + @"Bin\bin2";
        this.GraphDatabase.Open();
        this.KnowledgeSet = new KnowledgeBase.KnowledgeDatabase("GraphBase");
        this.KnowledgeSet.Ontology = this.GraphDatabase;
        KnowledgeBase.Reference.KnowledgeSet = this.KnowledgeSet;
    }

    public static WikiSystem GetWikiSystem()
    {
        if(wikisystem==null)
            throw new Exception("WikiSystem is not initialized!");
        return wikisystem;
    }

    NaturalWiki.Backend.RemotableMentorProxy mentorproxy;
    public NaturalWiki.Backend.RemotableMentorProxy GetMentorProxy()
    {
        if (mentorproxy == null)
            mentorproxy = new NaturalWiki.Backend.RemotableMentorProxy(this.GetMentor());
        return mentorproxy;
    }
    Semantics.Remoting.RemotableMentor mentor;
    public Semantics.Remoting.RemotableMentor GetMentor()
    {
        Semantics.Base.Reference.GetReference().GetMentorEvent += new Semantics.Base.Reference.GetMentorEventHandler(WikiSystem_GetMentorEvent);
        if(mentor==null)
            mentor = new Semantics.Remoting.RemotableMentor();
        return mentor;
    }

    KnowledgeBase.KnowledgeSet KnowledgeSet;
    KnowledgeBase.KnowledgeSet GetKnowledgeSet()
    {
        if (this.KnowledgeSet == null)
            throw new Exception("Not intitalized!");
        //if (this.KnowledgeSet == null)
        //{
        //    this.KnowledgeSet = new KnowledgeBase.KnowledgeDatabase("GraphBase");
        //    this.KnowledgeSet.Ontology = this.GraphDatabase;
        //}
        return this.KnowledgeSet;
    }
    Semantics.Base.IMentor WikiSystem_GetMentorEvent(string username)
    {
        return new Semantics.Training.Mentor(this.GetKnowledgeSet());
    }
}
